import 'dart:convert';

import 'package:shared_preferences/shared_preferences.dart';

class LocalStorage {
  /// ###设置数据
  /// ####[key] String|键名
  /// ####[value] 键值
  static setFormat(String key, value) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    value = json.encode(value);
    prefs.setString(key, value);
  }

  /// ###获取数据
  /// ####[key] String|键名
  static getFormat(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.getString(key);
  }

  /// ### 设置Double数据
  /// #### [key] String|必填|键名
  /// #### [value] double|必填|键值
  static setDouble(String key, double value) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setDouble(key, value);
  }

  /// ### 获取Double数据
  /// #### [key] String|必填|键名
  static getDouble(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.getDouble(key);
  }


  /// ### 设置Sting的值
  /// #### [key] String|必填|键名
  /// #### [value] String|必填|键值
  static setStringItem(String key, String value) async {
    final prefs = await SharedPreferences.getInstance();
    prefs.setString(key, value);
  }

  /// ### 获取返回为String的内容
  /// #### [value] String|必填|键值
  static getStringItem(String key) async {
    final prefs = await SharedPreferences.getInstance();
    String value = prefs.getString(key);
    return value;
  }


  /// ### 设置布尔的值
  /// #### [key] String|必填|键名
  /// #### [value] bool|必填|键值
  static setBoolItem(String key, bool value) async {
    final prefs = await SharedPreferences.getInstance();
    prefs.setBool(key, value);
  }

  // 获取返回为bool的内容
  static getBoolItem(String key) async {
    final prefs = await SharedPreferences.getInstance();
    bool value = prefs.getBool(key);
    return value;
  }


  /// ###设置StringList
  /// #### [key] String|必填|存储的名称
  /// #### [value] List|必填|存储的数据
  static setStringListItem(String key, List value) async {
    List<String> ls = new List();
    final prefs = await SharedPreferences.getInstance();
    value.forEach((e) => ls.add(json.encode(e)));
    prefs.setStringList(key, ls);
  }

  /// ### 获取返回为StringList的内容
  /// #### [key] String|必填|存储的名称
  static getStringListItem(String key) async {
    List<Map> ls = new List();
    final prefs = await SharedPreferences.getInstance();
    List<String> value = prefs.getStringList(key);
    if(value != null){
      value.forEach((e) => ls.add(json.decode(e)));
      return ls;
    }
  }


  /// ###移除单个
  /// #### [key] String|必填|存储的名称
  static remove(String key) async {
    final prefs = await SharedPreferences.getInstance();
    prefs.remove(key);
  }

  // ### 清空所有的
  static clear() async {
    final prefs = await SharedPreferences.getInstance();
    prefs.clear();
  }


}
